package com.cdks.wuxiu.api.component.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 配置类
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Resource
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("舞秀 API")
                .apiInfo(apiInfo())
                .globalOperationParameters(globalOperationParameters())
                .directModelSubstitute(LocalDateTime.class, String.class)
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .directModelSubstitute(BigDecimal.class, String.class)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .useDefaultResponseMessages(false)
                .extensions(openApiExtensionResolver.buildSettingExtensions());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("舞秀 API 文档")
                .description("舞秀 Java API")
                .version("1.0.0")
                .contact(new Contact("Link", "", "eolinker@gmail.com"))
                .build();
    }

    private List<Parameter> globalOperationParameters() {
        List<Parameter> list = new ArrayList<>();
        Parameter serial = new ParameterBuilder().name("serial")
                .description("自增版本号")
                .modelRef(new ModelRef("int"))
                .parameterType("header")
                .scalarExample(1)
                .required(true)
                .order(-100)
                .build();
        Parameter branch = new ParameterBuilder().name("branch")
                .description("渠道号")
                .modelRef(new ModelRef("int"))
                .parameterType("header")
                .scalarExample(1)
                .required(true)
                .order(-100)
                .build();
        Parameter sign = new ParameterBuilder().name("sign")
                .description("参数签名")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .scalarExample(null)
                .required(true)
                .order(-100)
                .build();
        list.add(serial);
        list.add(branch);
        list.add(sign);
        return list;
    }
}
